package com.fenxiangle.yueding.feature.publish.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.util.DateTool;
import com.fenxiangle.yueding.entity.bo.OrderBo;
import com.fenxiangle.yueding.entity.bo.PublishDemandBo;
import com.fenxiangle.yueding.feature.focus.view.PublishGenreActivity;
import com.fenxiangle.yueding.feature.home.view.SelectMapActivity;
import com.fenxiangle.yueding.feature.mine.view.RechargeCreditActivity;
import com.fenxiangle.yueding.feature.publish.contract.PublishContract;
import com.fenxiangle.yueding.feature.publish.dependencies.mine.DaggerPublishComponent;
import com.fenxiangle.yueding.feature.publish.dependencies.mine.PublishPresenterModule;
import com.fenxiangle.yueding.framework.DataServer;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.fenxiangle.yueding.common.util.DateTool.DEFAULT_SDF3;

public class PublishRequirementActivity extends BaseActivity implements PublishContract.View {
    @Inject
    PublishContract.Presenter mPresenter;

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_publish_position)
    TextView mTvPublishPosition;
    @BindView(R.id.edt_publish_desc)
    EditText mEdtPublishDesc;
    @BindView(R.id.edt_publish_num)
    EditText mEdtPublishNum;
    @BindView(R.id.tv_publish_starttime)
    TextView mTvPublishStarttime;
    @BindView(R.id.edt_publish_endtime)
    TextView mEdtPublishEndtime;
    @BindView(R.id.tv_publish_address)
    TextView mTvPublishAddress;
    @BindView(R.id.edt_publish_pay_one)
    EditText mEdtPublishPayOne;
    @BindView(R.id.edt_publish_pay_all)
    EditText mEdtPublishPayAll;
    @BindView(R.id.tv_publish_position_tips)
    TextView mTvPublishPositionTips;
    @BindView(R.id.btn_to_publish)
    TextView mBtnToPublish;
    private PoiItem mPoiItem;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_publish_requirement;
    }

    @Override
    protected void initInjector() {
        DaggerPublishComponent.builder().publishPresenterModule(new PublishPresenterModule(this)).build().inject(this);
    }

    InputMethodManager imm;

    @Override
    protected void initView() {
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        initToolBar(mToolbar, "发布需求", true, true);
    }

    @Override
    protected void initData() {

    }


    @OnClick({R.id.tv_publish_position, R.id.tv_publish_starttime, R.id.edt_publish_endtime, R.id.tv_publish_address, R.id.btn_to_publish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_publish_position:
                startActivityForResult(new Intent(this, PublishGenreActivity.class), 2);
                break;
            case R.id.tv_publish_starttime:
                imm.hideSoftInputFromWindow(mTvPublishStarttime.getWindowToken(), 0); //强制隐藏键盘
                TimePickerView pvStart = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        mTvPublishStarttime.setText(DateTool.date2String(date, DEFAULT_SDF3));
                    }
                })
                        .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                        .setLineSpacingMultiplier(1.8f)
                        .build();
                pvStart.show();
                break;
            case R.id.edt_publish_endtime:
                imm.hideSoftInputFromWindow(mEdtPublishEndtime.getWindowToken(), 0); //强制隐藏键盘
                TimePickerView pvEnd = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        mEdtPublishEndtime.setText(DateTool.date2String(date, DEFAULT_SDF3));
                    }
                })
                        .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                        .setLineSpacingMultiplier(1.8f)
                        .build();
                pvEnd.show();
                break;
            case R.id.tv_publish_address:
                startActivityForResult(new Intent(this, SelectMapActivity.class), 3);
                break;
            case R.id.btn_to_publish:
                mPresenter.publishDemand(1,
                        mTvPublishPosition.getText().toString(),
                        mEdtPublishDesc.getText().toString(),
                        mEdtPublishNum.getText().toString(),
                        mTvPublishStarttime.getText().toString(),
                        mEdtPublishEndtime.getText().toString(),
                        mPoiItem,
                        mEdtPublishPayOne.getText().toString(),
                        "",
                        100
                );
                break;
        }
    }

    @Override
    public void showPublishSuccess(OrderBo s) {
        Intent intent = new Intent(this, PayActivity.class);
        intent.putExtra("no", s);
        startActivity(intent);
        finish();
//        showMsg("showPublishSuccess");
    }

    @Override
    public void showError(String error) {
        showMsg(error);
    }

    @Override
    public void showInsufficientBalance() {
        startActivity(new Intent(this, RechargeCreditActivity.class));
    }

    @Override
    public void showPublishListSuccess(List<PublishDemandBo> workBoPageData) {

    }

    @Override
    public void showPublishListError(String error) {

    }

    @Override
    public void showPublishListMoreSuccess(List<PublishDemandBo> msg) {

    }

    @Override
    public void showPublishListMoreError(String msg) {

    }

    @Override
    public void showPublishListEmpty() {

    }

    @Override
    public void showcancleOrderSuccess() {

    }

    @Override
    public void showConfrimOrderSuccess() {

    }

    @Override
    public void showConfrimTxSuccess() {

    }

    @Override
    public void showWeiQunanOrderSuccess() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 2:
                if (data != null && !TextUtils.isEmpty((data.getStringExtra("three")))) {
                    mTvPublishPosition.setText(data.getStringExtra("three"));
                }
                break;
            case 3:
                if (data != null && data.getParcelableExtra("poi_item") != null) {
                    mPoiItem = data.getParcelableExtra("poi_item");
                    mTvPublishAddress.setText(mPoiItem.getTitle());
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
